package com.jay.io.clazz;

import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class PipedStreamCls {
	public static void main(String[] args) {
		PipedInputStream pis = new PipedInputStream();
		PipedOutputStream pos = new PipedOutputStream();
		try {
			pos.connect(pis);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Producer producer = new Producer(pos);
		Consumer con = new Consumer(pis);
		producer.start();
		con.start();
	}
}

class Producer extends Thread {

	PipedOutputStream pos;

	public Producer(PipedOutputStream pos) {
		this.pos = pos;
	}

	public void run() {
		try {
			pos.write("Producer generator .".getBytes());
			for (int i = 0; i < 100; i++) {
				pos.write(i);
				sleep(300);
			}
			pos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}

class Consumer extends Thread {

	PipedInputStream pis;

	public Consumer(PipedInputStream pis) {
		this.pis = pis;
	}

	public void run() {

		try {
			int data;
			while ((data = pis.read()) != -1) {
				System.out.println(data);
			}
			System.out.println(data);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
